/*     */ package com.zimbra.cs.filter;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.zimbra.common.filter.Sieve.Condition;
/*     */ import com.zimbra.common.filter.Sieve.DateComparison;
/*     */ import com.zimbra.common.filter.Sieve.StringComparison;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.soap.mail.type.FilterAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.FileIntoAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.FlagAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.NotifyAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.RedirectAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.ReplyAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.TagAction;
/*     */ import com.zimbra.soap.mail.type.FilterRule;
/*     */ import com.zimbra.soap.mail.type.FilterTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.AddressBookTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.AddressTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.BodyTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.ContactRankingTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.CurrentDayOfWeekTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.CurrentTimeTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.DateTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.FlaggedTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.HeaderTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.ImportanceTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.InviteTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.MeTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.MimeHeaderTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.SizeTest;
/*     */ import com.zimbra.soap.mail.type.FilterTests;
/*     */ import com.zimbra.soap.mail.type.NestedRule;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.TreeMap;
/*     */ 
/*     */ public final class SoapToSieve
/*     */ {
/*     */   private final List<FilterRule> rules;
/*     */   private StringBuilder buffer;
/*     */   
/*     */   public SoapToSieve(List<FilterRule> rules)
/*     */   {
/*  45 */     this.rules = rules;
/*     */   }
/*     */   
/*     */   public String getSieveScript() throws ServiceException {
/*  49 */     if (this.buffer == null) {
/*  50 */       this.buffer = new StringBuilder();
/*  51 */       this.buffer.append("require [\"fileinto\", \"reject\", \"tag\", \"flag\"];\n");
/*  52 */       for (FilterRule rule : this.rules) {
/*  53 */         this.buffer.append('\n');
/*  54 */         handleRule(rule);
/*     */       }
/*     */     }
/*  57 */     return this.buffer.toString();
/*     */   }
/*     */   
/*     */   private void handleRule(FilterRule rule) throws ServiceException {
/*  61 */     String name = rule.getName();
/*  62 */     boolean active = rule.isActive();
/*     */     
/*  64 */     FilterTests tests = rule.getFilterTests();
/*  65 */     Sieve.Condition condition = Sieve.Condition.fromString(tests.getCondition());
/*  66 */     if (condition == null) {
/*  67 */       condition = Sieve.Condition.allof;
/*     */     }
/*     */     
/*     */ 
/*  71 */     this.buffer.append("# ").append(name).append('\n');
/*  72 */     if (active) {
/*  73 */       this.buffer.append("if ");
/*     */     } else {
/*  75 */       this.buffer.append("disabled_if ");
/*     */     }
/*  77 */     this.buffer.append(condition).append(" (");
/*     */     
/*     */ 
/*  80 */     Map<Integer, String> index2test = new TreeMap();
/*  81 */     for (FilterTest test : tests.getTests()) {
/*  82 */       String result = handleTest(test);
/*  83 */       if (result != null) {
/*  84 */         FilterUtil.addToMap(index2test, test.getIndex(), result);
/*     */       }
/*     */     }
/*  87 */     Joiner.on(",\n  ").appendTo(this.buffer, index2test.values());
/*  88 */     this.buffer.append(") {\n");
/*     */     
/*     */ 
/*  91 */     NestedRule child = rule.getChild();
/*  92 */     if (child != null)
/*     */     {
/*  94 */       String nestedRuleBlock = handleNest("    ", child);
/*  95 */       this.buffer.append(nestedRuleBlock);
/*     */     }
/*     */     
/*     */ 
/*  99 */     Map<Integer, String> index2action = new TreeMap();
/* 100 */     List<FilterAction> filterActions = rule.getFilterActions();
/* 101 */     if (filterActions == null) {
/* 102 */       if (child == null)
/*     */       {
/* 104 */         throw ServiceException.INVALID_REQUEST("Missing action", null);
/*     */       }
/*     */       
/*     */ 
/* 108 */       this.buffer.append("}\n");
/* 109 */       return;
/*     */     }
/*     */     
/* 112 */     for (FilterAction action : filterActions) {
/* 113 */       String result = handleAction(action);
/* 114 */       if (result != null) {
/* 115 */         FilterUtil.addToMap(index2action, action.getIndex(), result);
/*     */       }
/*     */     }
/* 118 */     for (String action : index2action.values()) {
/* 119 */       this.buffer.append("    ").append(action).append(";\n");
/*     */     }
/* 121 */     this.buffer.append("}\n");
/*     */   }
/*     */   
/*     */   private String handleNest(String baseIndents, NestedRule currentNestedRule)
/*     */     throws ServiceException
/*     */   {
/* 127 */     StringBuilder nestedIfBlock = new StringBuilder();
/* 128 */     nestedIfBlock.append(baseIndents);
/*     */     
/* 130 */     Sieve.Condition childCondition = Sieve.Condition.fromString(currentNestedRule.getFilterTests().getCondition());
/*     */     
/* 132 */     if (childCondition == null) {
/* 133 */       childCondition = Sieve.Condition.allof;
/*     */     }
/*     */     
/*     */ 
/* 137 */     nestedIfBlock.append("if ");
/* 138 */     nestedIfBlock.append(childCondition).append(" (");
/*     */     
/*     */ 
/* 141 */     Map<Integer, String> index2childTest = new TreeMap();
/* 142 */     for (FilterTest childTest : currentNestedRule.getFilterTests().getTests()) {
/* 143 */       String childResult = handleTest(childTest);
/* 144 */       if (childResult != null) {
/* 145 */         FilterUtil.addToMap(index2childTest, childTest.getIndex(), childResult);
/*     */       }
/*     */     }
/* 148 */     Joiner.on(",\n  " + baseIndents).appendTo(nestedIfBlock, index2childTest.values());
/* 149 */     nestedIfBlock.append(") {\n");
/*     */     
/*     */ 
/* 152 */     if (currentNestedRule.getChild() != null) {
/* 153 */       nestedIfBlock.append(handleNest(baseIndents + "    ", currentNestedRule.getChild()));
/*     */     }
/*     */     
/*     */ 
/* 157 */     Map<Integer, String> index2childAction = new TreeMap();
/* 158 */     List<FilterAction> childActions = currentNestedRule.getFilterActions();
/* 159 */     if (childActions == null) {
/* 160 */       if (currentNestedRule.getChild() == null)
/*     */       {
/* 162 */         throw ServiceException.INVALID_REQUEST("Missing action", null);
/*     */       }
/*     */       
/*     */ 
/* 166 */       nestedIfBlock.append(baseIndents);
/* 167 */       nestedIfBlock.append("}\n");
/*     */       
/* 169 */       return nestedIfBlock.toString();
/*     */     }
/*     */     
/* 172 */     for (FilterAction childAction : childActions) {
/* 173 */       String childResult = handleAction(childAction);
/* 174 */       if (childResult != null) {
/* 175 */         FilterUtil.addToMap(index2childAction, childAction.getIndex(), childResult);
/*     */       }
/*     */     }
/* 178 */     for (String childAction : index2childAction.values()) {
/* 179 */       nestedIfBlock.append(baseIndents);
/* 180 */       nestedIfBlock.append("    ").append(childAction).append(";\n");
/*     */     }
/*     */     
/* 183 */     nestedIfBlock.append(baseIndents);
/* 184 */     nestedIfBlock.append("}\n");
/*     */     
/* 186 */     return nestedIfBlock.toString();
/*     */   }
/*     */   
/*     */   private String handleTest(FilterTest test) throws ServiceException
/*     */   {
/* 191 */     String snippet = null;
/*     */     
/* 193 */     if ((test instanceof FilterTest.HeaderTest)) {
/* 194 */       snippet = toSieve((FilterTest.HeaderTest)test);
/* 195 */     } else if ((test instanceof FilterTest.MimeHeaderTest)) {
/* 196 */       snippet = toSieve((FilterTest.MimeHeaderTest)test);
/* 197 */     } else if ((test instanceof FilterTest.AddressTest)) {
/* 198 */       snippet = toSieve((FilterTest.AddressTest)test);
/* 199 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.HeaderExistsTest)) {
/* 200 */       String header = ((com.zimbra.soap.mail.type.FilterTest.HeaderExistsTest)test).getHeader();
/* 201 */       snippet = String.format("exists \"%s\"", new Object[] { FilterUtil.escape(header) });
/* 202 */     } else if ((test instanceof FilterTest.SizeTest)) {
/* 203 */       FilterTest.SizeTest sizeTest = (FilterTest.SizeTest)test;
/* 204 */       com.zimbra.common.filter.Sieve.NumberComparison comp = com.zimbra.common.filter.Sieve.NumberComparison.fromString(sizeTest.getNumberComparison());
/* 205 */       String size = sizeTest.getSize();
/*     */       try {
/* 207 */         FilterUtil.parseSize(size);
/*     */       } catch (NumberFormatException e) {
/* 209 */         throw ServiceException.INVALID_REQUEST("Invalid size: " + size, e);
/*     */       }
/* 211 */       snippet = String.format("size :%s %s", new Object[] { comp, size });
/* 212 */     } else if ((test instanceof FilterTest.DateTest)) {
/* 213 */       FilterTest.DateTest dateTest = (FilterTest.DateTest)test;
/* 214 */       Sieve.DateComparison comp = Sieve.DateComparison.fromString(dateTest.getDateComparison());
/* 215 */       java.util.Date date = new java.util.Date(dateTest.getDate().longValue() * 1000L);
/* 216 */       snippet = String.format("date :%s \"%s\"", new Object[] { comp, com.zimbra.common.filter.Sieve.DATE_PARSER.format(date) });
/* 217 */     } else if ((test instanceof FilterTest.BodyTest)) {
/* 218 */       FilterTest.BodyTest bodyTest = (FilterTest.BodyTest)test;
/* 219 */       String format = bodyTest.isCaseSensitive() ? "body :contains :comparator \"i;octet\" \"%s\"" : "body :contains \"%s\"";
/*     */       
/* 221 */       snippet = String.format(format, new Object[] { FilterUtil.escape(bodyTest.getValue()) });
/* 222 */     } else if ((test instanceof FilterTest.AddressBookTest)) {
/* 223 */       FilterTest.AddressBookTest abTest = (FilterTest.AddressBookTest)test;
/* 224 */       snippet = "addressbook :in \"" + FilterUtil.escape(abTest.getHeader()) + '"';
/* 225 */     } else if ((test instanceof FilterTest.ContactRankingTest)) {
/* 226 */       FilterTest.ContactRankingTest rankingTest = (FilterTest.ContactRankingTest)test;
/* 227 */       snippet = "contact_ranking :in \"" + FilterUtil.escape(rankingTest.getHeader()) + '"';
/* 228 */     } else if ((test instanceof FilterTest.MeTest)) {
/* 229 */       FilterTest.MeTest meTest = (FilterTest.MeTest)test;
/* 230 */       snippet = "me :in \"" + FilterUtil.escape(meTest.getHeader()) + '"';
/* 231 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.AttachmentTest)) {
/* 232 */       snippet = "attachment";
/* 233 */     } else if ((test instanceof FilterTest.InviteTest)) {
/* 234 */       snippet = toSieve((FilterTest.InviteTest)test);
/* 235 */     } else if ((test instanceof FilterTest.CurrentTimeTest)) {
/* 236 */       FilterTest.CurrentTimeTest timeTest = (FilterTest.CurrentTimeTest)test;
/* 237 */       Sieve.DateComparison comparison = Sieve.DateComparison.fromString(timeTest.getDateComparison());
/* 238 */       String time = timeTest.getTime();
/*     */       
/* 240 */       String timeFormat = "HHmm";
/* 241 */       java.text.SimpleDateFormat parser = new java.text.SimpleDateFormat(timeFormat);
/* 242 */       parser.setLenient(false);
/*     */       try {
/* 244 */         parser.parse(time);
/*     */       } catch (java.text.ParseException e) {
/* 246 */         throw ServiceException.INVALID_REQUEST("Invalid time: " + time, e);
/*     */       }
/* 248 */       if (time.length() != timeFormat.length()) {
/* 249 */         throw ServiceException.INVALID_REQUEST("Time string must be of length " + timeFormat.length(), null);
/*     */       }
/* 251 */       snippet = String.format("current_time :%s \"%s\"", new Object[] { comparison, time });
/* 252 */     } else if ((test instanceof FilterTest.CurrentDayOfWeekTest)) {
/* 253 */       FilterTest.CurrentDayOfWeekTest dayTest = (FilterTest.CurrentDayOfWeekTest)test;
/* 254 */       String value = dayTest.getValues();
/* 255 */       String[] daysOfWeek = value.split(",");
/* 256 */       for (int i = 0; i < daysOfWeek.length; i++)
/*     */       {
/*     */         try {
/* 259 */           int day = Integer.valueOf(daysOfWeek[i]).intValue();
/* 260 */           if ((day < 0) || (day > 6)) {
/* 261 */             throw ServiceException.INVALID_REQUEST("Day of week index must be from 0 (Sunday) to 6 (Saturday)", null);
/*     */           }
/*     */         }
/*     */         catch (NumberFormatException e) {
/* 265 */           throw ServiceException.INVALID_REQUEST("Invalid day of week index: " + daysOfWeek[i], e);
/*     */         }
/*     */         
/* 268 */         daysOfWeek[i] = StringUtil.enclose(daysOfWeek[i], '"');
/*     */       }
/* 270 */       snippet = "current_day_of_week :is [" + Joiner.on(',').join(daysOfWeek) + "]";
/* 271 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.ConversationTest)) {
/* 272 */       com.zimbra.soap.mail.type.FilterTest.ConversationTest convTest = (com.zimbra.soap.mail.type.FilterTest.ConversationTest)test;
/* 273 */       String where = (String)com.google.common.base.Objects.firstNonNull(convTest.getWhere(), "started");
/* 274 */       snippet = String.format("conversation :where \"%s\"", new Object[] { FilterUtil.escape(where) });
/* 275 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.FacebookTest)) {
/* 276 */       snippet = "facebook";
/* 277 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.LinkedInTest)) {
/* 278 */       snippet = "linkedin";
/* 279 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.SocialcastTest)) {
/* 280 */       snippet = "socialcast";
/* 281 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.TwitterTest)) {
/* 282 */       snippet = "twitter";
/* 283 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.CommunityRequestsTest)) {
/* 284 */       snippet = "community_requests";
/* 285 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.CommunityContentTest)) {
/* 286 */       snippet = "community_content";
/* 287 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.CommunityConnectionsTest)) {
/* 288 */       snippet = "community_connections";
/* 289 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.ListTest)) {
/* 290 */       snippet = "list";
/* 291 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.BulkTest)) {
/* 292 */       snippet = "bulk";
/* 293 */     } else if ((test instanceof FilterTest.ImportanceTest)) {
/* 294 */       FilterTest.ImportanceTest impTest = (FilterTest.ImportanceTest)test;
/* 295 */       snippet = String.format("importance \"%s\"", new Object[] { impTest.getImportance() });
/* 296 */     } else if ((test instanceof FilterTest.FlaggedTest)) {
/* 297 */       FilterTest.FlaggedTest flaggedTest = (FilterTest.FlaggedTest)test;
/* 298 */       snippet = "flagged \"" + flaggedTest.getFlag() + "\"";
/* 299 */     } else if ((test instanceof com.zimbra.soap.mail.type.FilterTest.TrueTest)) {
/* 300 */       snippet = "true";
/*     */     } else {
/* 302 */       com.zimbra.common.util.ZimbraLog.soap.debug("Ignoring unexpected test: %s", new Object[] { test });
/*     */     }
/*     */     
/* 305 */     if ((snippet != null) && (test.isNegative())) {
/* 306 */       snippet = "not " + snippet;
/*     */     }
/* 308 */     return snippet;
/*     */   }
/*     */   
/*     */   private static String toSieve(FilterTest.HeaderTest test) throws ServiceException {
/* 312 */     String header = getSieveHeaderList(test.getHeaders());
/* 313 */     Sieve.StringComparison comp = Sieve.StringComparison.fromString(test.getStringComparison());
/* 314 */     if (com.google.common.base.Strings.isNullOrEmpty(test.getValue())) {
/* 315 */       throw ServiceException.INVALID_REQUEST("missing required attribute: value", null);
/*     */     }
/* 317 */     return toSieve("header", header, comp, test.isCaseSensitive(), test.getValue());
/*     */   }
/*     */   
/*     */   private static String toSieve(FilterTest.MimeHeaderTest test) throws ServiceException {
/* 321 */     String header = getSieveHeaderList(test.getHeaders());
/* 322 */     Sieve.StringComparison comp = Sieve.StringComparison.fromString(test.getStringComparison());
/* 323 */     return toSieve("mime_header", header, comp, test.isCaseSensitive(), test.getValue());
/*     */   }
/*     */   
/*     */   private static String toSieve(String name, String header, Sieve.StringComparison comp, boolean caseSensitive, String value) throws ServiceException
/*     */   {
/* 328 */     checkValue(comp, value);
/* 329 */     String format = caseSensitive ? "%s :%s :comparator \"" + com.zimbra.common.filter.Sieve.Comparator.ioctet + "\" %s \"%s\"" : "%s :%s %s \"%s\"";
/*     */     
/* 331 */     return String.format(format, new Object[] { name, comp, header, FilterUtil.escape(value) });
/*     */   }
/*     */   
/*     */   private static String toSieve(FilterTest.AddressTest test) throws ServiceException {
/* 335 */     String header = getSieveHeaderList(test.getHeader());
/* 336 */     com.zimbra.common.filter.Sieve.AddressPart part = com.zimbra.common.filter.Sieve.AddressPart.fromString(test.getPart());
/* 337 */     if (part == null) {
/* 338 */       part = com.zimbra.common.filter.Sieve.AddressPart.all;
/*     */     }
/* 340 */     Sieve.StringComparison comp = Sieve.StringComparison.fromString(test.getStringComparison());
/* 341 */     String value = test.getValue();
/* 342 */     checkValue(comp, value);
/* 343 */     return String.format("address :%s :%s :comparator \"%s\" %s \"%s\"", new Object[] { part, comp, test.isCaseSensitive() ? com.zimbra.common.filter.Sieve.Comparator.ioctet : com.zimbra.common.filter.Sieve.Comparator.iasciicasemap, header, FilterUtil.escape(value) });
/*     */   }
/*     */   
/*     */ 
/*     */   private static void checkValue(Sieve.StringComparison comparison, String value)
/*     */     throws ServiceException
/*     */   {
/* 350 */     if ((comparison == Sieve.StringComparison.matches) && (value != null) && (value.contains("*****"))) {
/* 351 */       throw ServiceException.INVALID_REQUEST("Wildcard match value cannot contain more than four asterisks in a row.", null);
/*     */     }
/*     */   }
/*     */   
/*     */   private static String getSieveHeaderList(String header) throws ServiceException
/*     */   {
/* 357 */     if (header.isEmpty()) {
/* 358 */       throw ServiceException.INVALID_REQUEST("header value is empty", null);
/*     */     }
/* 360 */     return getSieveMultiValue(header);
/*     */   }
/*     */   
/*     */   private static String getSieveMultiValue(String s) {
/* 364 */     String[] values = new String[0];
/*     */     
/* 366 */     if (!s.isEmpty()) {
/* 367 */       values = s.split(",");
/* 368 */       for (int i = 0; i < values.length; i++) {
/* 369 */         values[i] = StringUtil.enclose(FilterUtil.escape(values[i]), '"');
/*     */       }
/*     */     }
/* 372 */     return '[' + StringUtil.join(",", values) + ']';
/*     */   }
/*     */   
/*     */   private static String toSieve(FilterTest.InviteTest test) {
/* 376 */     StringBuilder buf = new StringBuilder("invite");
/* 377 */     List<String> methods = test.getMethods();
/* 378 */     if (!methods.isEmpty()) {
/* 379 */       buf.append(" :method [");
/* 380 */       boolean firstTime = true;
/* 381 */       for (String method : methods) {
/* 382 */         if (firstTime) {
/* 383 */           firstTime = false;
/*     */         } else {
/* 385 */           buf.append(", ");
/*     */         }
/* 387 */         buf.append('"');
/* 388 */         buf.append(FilterUtil.escape(method));
/* 389 */         buf.append('"');
/*     */       }
/* 391 */       buf.append("]");
/*     */     }
/* 393 */     return buf.toString();
/*     */   }
/*     */   
/*     */   private static String handleAction(FilterAction action) throws ServiceException {
/* 397 */     if ((action instanceof com.zimbra.soap.mail.type.FilterAction.KeepAction))
/* 398 */       return "keep";
/* 399 */     if ((action instanceof com.zimbra.soap.mail.type.FilterAction.DiscardAction))
/* 400 */       return "discard";
/* 401 */     if ((action instanceof FilterAction.FileIntoAction)) {
/* 402 */       FilterAction.FileIntoAction fileinto = (FilterAction.FileIntoAction)action;
/* 403 */       String folderPath = fileinto.getFolder();
/* 404 */       if (StringUtil.isNullOrEmpty(folderPath)) {
/* 405 */         throw ServiceException.INVALID_REQUEST("Missing folderPath", null);
/*     */       }
/* 407 */       return String.format("fileinto \"%s\"", new Object[] { FilterUtil.escape(folderPath) }); }
/* 408 */     if ((action instanceof FilterAction.TagAction)) {
/* 409 */       FilterAction.TagAction tag = (FilterAction.TagAction)action;
/* 410 */       String tagName = tag.getTag();
/* 411 */       if (StringUtil.isNullOrEmpty(tagName)) {
/* 412 */         throw ServiceException.INVALID_REQUEST("Missing tag", null);
/*     */       }
/* 414 */       return String.format("tag \"%s\"", new Object[] { FilterUtil.escape(tagName) }); }
/* 415 */     if ((action instanceof FilterAction.FlagAction)) {
/* 416 */       FilterAction.FlagAction flag = (FilterAction.FlagAction)action;
/* 417 */       String flagName = flag.getFlag();
/* 418 */       if (StringUtil.isNullOrEmpty(flagName)) {
/* 419 */         throw ServiceException.INVALID_REQUEST("Missing flag", null);
/*     */       }
/* 421 */       return String.format("flag \"%s\"", new Object[] { com.zimbra.common.filter.Sieve.Flag.valueOf(flagName) }); }
/* 422 */     if ((action instanceof FilterAction.RedirectAction)) {
/* 423 */       FilterAction.RedirectAction redirect = (FilterAction.RedirectAction)action;
/* 424 */       String address = redirect.getAddress();
/* 425 */       if (StringUtil.isNullOrEmpty(address)) {
/* 426 */         throw ServiceException.INVALID_REQUEST("Missing address", null);
/*     */       }
/* 428 */       return String.format("redirect \"%s\"", new Object[] { FilterUtil.escape(address) }); }
/* 429 */     if ((action instanceof FilterAction.ReplyAction)) {
/* 430 */       FilterAction.ReplyAction reply = (FilterAction.ReplyAction)action;
/* 431 */       String content = reply.getContent();
/* 432 */       if (StringUtil.isNullOrEmpty(content)) {
/* 433 */         throw ServiceException.INVALID_REQUEST("Missing reply content", null);
/*     */       }
/* 435 */       return "reply text:\r\n" + getDotStuffed(content) + "\r\n.\r\n";
/*     */     }
/*     */     
/*     */ 
/* 439 */     if ((action instanceof FilterAction.NotifyAction)) {
/* 440 */       FilterAction.NotifyAction notify = (FilterAction.NotifyAction)action;
/* 441 */       String emailAddr = notify.getAddress();
/* 442 */       if (StringUtil.isNullOrEmpty(emailAddr)) {
/* 443 */         throw ServiceException.INVALID_REQUEST("Missing address", null);
/*     */       }
/* 445 */       String subjectTemplate = com.google.common.base.Strings.nullToEmpty(notify.getSubject());
/* 446 */       String bodyTemplate = com.google.common.base.Strings.nullToEmpty(notify.getContent());
/* 447 */       int maxBodyBytes = ((Integer)com.google.common.base.Objects.firstNonNull(Integer.valueOf(notify.getMaxBodySize()), Integer.valueOf(-1))).intValue();
/* 448 */       String origHeaders = com.google.common.base.Strings.nullToEmpty(notify.getOrigHeaders());
/* 449 */       if ((!subjectTemplate.isEmpty()) && (containsSubjectHeader(origHeaders))) {
/* 450 */         throw ServiceException.INVALID_REQUEST("subject conflict", null);
/*     */       }
/* 452 */       return "notify " + StringUtil.enclose(emailAddr, '"') + ' ' + StringUtil.enclose(subjectTemplate, '"') + ' ' + "text:\r\n" + getDotStuffed(bodyTemplate) + "\r\n.\r\n" + (maxBodyBytes < 0 ? "" : new StringBuilder().append(" ").append(maxBodyBytes).toString()) + (origHeaders.isEmpty() ? "" : new StringBuilder().append(" ").append(getSieveMultiValue(origHeaders)).toString());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 458 */     if ((action instanceof com.zimbra.soap.mail.type.FilterAction.StopAction)) {
/* 459 */       return "stop";
/*     */     }
/* 461 */     com.zimbra.common.util.ZimbraLog.soap.debug("Ignoring unexpected action: %s", new Object[] { action });
/*     */     
/* 463 */     return null;
/*     */   }
/*     */   
/*     */   private static boolean containsSubjectHeader(String origHeaders) {
/* 467 */     for (String header : origHeaders.split(",")) {
/* 468 */       if ("Subject".equalsIgnoreCase(header)) {
/* 469 */         return true;
/*     */       }
/*     */     }
/* 472 */     return false;
/*     */   }
/*     */   
/*     */   private static String getDotStuffed(String bodyTemplate) {
/* 476 */     return bodyTemplate.replaceAll("\r\n\\.", "\r\n..");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/SoapToSieve.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */